import os
from time import sleep

import tornado

from db.model import Article
from db.user import User
from libs.Tools import TimeTools
from view.list_sub import isIgnore


class NewsPage(tornado.web.RequestHandler):
    def get(self):
        User.db.connect()
        articles = Article.select().order_by(Article.date.desc())
        article_list = []
        for art in articles:
            if isIgnore(art.path):
                continue
            article_list.append({
                'title': art.title,
                'a': '/article' + art.path[:-3],
                'read_count': art.read_count,
                'id': art.id,
                'time': TimeTools.getDifferText(art.date),
                'comment_count': art.comment_count,
                'like_count': art.like_count,
                'repost_count': art.repost_count
            })

        User.db.close()
        self.render('list-news.html', article_list=article_list)


def get_news_page():
    articles = Article.select().order_by(Article.date.desc())
    article_list = []
    for art in articles:
        if isIgnore(art.path):
            continue
        article_list.append({
            'title': art.title,
            'a': '/article' + art.path[:-3],
            'read_count': art.read_count,
            'id': art.id,
            'time': TimeTools.getDifferText(art.date),
            'comment_count': art.comment_count,
            'like_count': art.like_count,
            'repost_count': art.repost_count
        })
    return article_list
